package org.dromara.wms.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseService;
import org.dromara.wms.domain.WmsOrderLine;
import org.dromara.wms.domain.WmsWarehouse;
import org.dromara.wms.domain.WmsWarehouseProduct;
import org.dromara.wms.mapper.WmsWarehouseMapper;
import org.dromara.wms.mapper.WmsWarehouseProductMapper;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 仓库管理Service业务层处理
 *
 * @author wenrui
 * @date 2024-03-22
 */
@RequiredArgsConstructor
@Service
public class WmsWarehouseProductServiceImpl extends BaseService {

    private final WmsWarehouseProductMapper baseMapper;

  /**
   * 查询 By whId
   */
  public WmsWarehouseProduct queryById(Long id){
    return baseMapper.selectById(id);
  }

    /**
     * 查询
     */
    public TableDataInfo<WmsWarehouseProduct> queryList(WmsWarehouseProduct bo, PageQuery pageQuery) {
      Page<WmsWarehouseProduct> result = baseMapper.getWarehouseProductList(pageQuery.build(), bo);
      return TableDataInfo.build(result);
    }

    /**
     * 新增/修改
     */
    public Boolean insertByBo(WmsWarehouseProduct[] bo) {
      for (WmsWarehouseProduct item : bo) {
        WmsWarehouseProduct wmsWarehouseProductList = baseMapper.selectOne(buildQueryWrapper(item));
        if (wmsWarehouseProductList != null){
          baseMapper.update(item,buildQueryWrapper(item));
        }else{
          baseMapper.insert(item);
        }
      }
      return true;
    }

  private LambdaQueryWrapper<WmsWarehouseProduct> buildQueryWrapper(WmsWarehouseProduct bo) {
    LambdaQueryWrapper<WmsWarehouseProduct> lqw = Wrappers.lambdaQuery();
    lqw.eq(WmsWarehouseProduct::getWhId, bo.getWhId());
    lqw.eq(WmsWarehouseProduct::getProductId, bo.getProductId());
    return lqw;
  }

  /**
   * 删除
   */
  public Boolean deleteById(WmsWarehouseProduct bo) {
    return baseMapper.delete(buildQueryWrapper(bo)) > 0;
  }

}
